/**
 * Copyright (c) 2010 icer He <icerhe@gmail.com>
 */
package com.icerhe.trigger.task;

import org.json.JSONException;

import com.icerhe.trigger.Consts;
import com.icerhe.trigger.ListActivity;
import com.icerhe.trigger.R;

import android.app.KeyguardManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;

/**
 * @author icer
 * 定时飞行模式
 */
public class AirplaneModeTask extends CronTask {
	
	public static final int NOTIFICATION_TYPE = 2;
	public static final String AIRPLANE_MODE_STATE = "airplane_mode_state";
	
	public static final int ON  = 1; //打开飞信模式
	public static final int OFF = 0; //退出飞行模式
	
	public AirplaneModeTask(Context context){
		super();
		title = context.getString(R.string.default_airplane_label);
		parse(context.getText(R.string.alarm_default_cronpatten).toString());
	}
	
	public AirplaneModeTask(Cursor c) {
		super(c);	
	}

	/* (non-Javadoc)
	 * @see com.icerhe.trigger.task.Task#runTask(android.content.Context)
	 */
	@Override
	public void runTask(Context context) {
		
		//如果在通话状态则放弃任务
		TelephonyManager telMg = 
			(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		if (telMg.getCallState() != TelephonyManager.CALL_STATE_IDLE) {
			return;
		}
		
		int state = getAirplaneMode();
		int currentState = getCurrentState(context);
		if ((state==ON || state==OFF) && (state!=currentState) ) {
			//当不在锁定状态(用户正在使用),使用弹出窗口(or Notification)提醒用户,
		    //还有多少时间(30sec倒计时)进入飞行状态.可取消
			KeyguardManager km = (KeyguardManager)context.getSystemService(
					Context.KEYGUARD_SERVICE);
			if (!km.inKeyguardRestrictedInputMode()) {
				Intent intent = new Intent(context, AirplaneModeDialog.class);
				intent.putExtra(INTENT_TASK_ID, id);   
				intent.setFlags(
		        		Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				context.startActivity(intent);
			} else {
				acceptAirplaneMode(context);
			}
		}

	}
	
	public void acceptAirplaneMode(Context context) {
		int state = getAirplaneMode();
		Settings.System.putInt(context.getContentResolver(),
				Settings.System.AIRPLANE_MODE_ON, state);
		Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
		intent.putExtra("state", state);
		context.sendBroadcast(intent);
		Log.d(Consts.TAG, state==ON?"AirplaneMode ON":"AirplaneMode OFF");
	}
	
    private static int getCurrentState(Context context) {
    	return Settings.System.getInt(
    			context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
    }
	
	public int getAirplaneMode() {
		try {
			return extra.has(AIRPLANE_MODE_STATE)?extra.getInt(AIRPLANE_MODE_STATE):1;
		} catch (JSONException e) {
			Log.e(Consts.TAG, "Fail to get value: airplane mode state");
			Log.e(Consts.TAG, Log.getStackTraceString(e));
		}	
		return -1;
	}
	
	public void setAirplaneMode(int state) {
		try {
			extra.put(AIRPLANE_MODE_STATE, state);
		} catch (JSONException e) {
			Log.e(Consts.TAG, "Fail to set value: airplane mode state");
			Log.e(Consts.TAG, Log.getStackTraceString(e));
		}
	}

}
